package com.wdl.sm.webui;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.boot.Banner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.wdl.sm.webui.security.SUser;
import com.wdl.sm.webui.security.SUserRepository;

@SpringBootApplication(scanBasePackages = "com.wdl.sm.webui")
public class UiApplication implements CommandLineRunner {
    private static final Logger LOG = LoggerFactory.getLogger(UiApplication.class);

    @Autowired
    private SUserRepository repository;

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(UiApplication.class);
        application.setBannerMode(Banner.Mode.OFF);
        application.run(args);
    }

    @Override
    public void run(String... arg0) throws Exception {
        SUser admin = repository.findByName("admin");
        if (admin == null) {
            LOG.info("User admin does not exist, create one!");
            repository.save(new SUser("admin", "ADMIN", "admin"));
        }

        SUser demo = repository.findByName("demo");
        if (demo == null) {
            LOG.info("User demo does not exist, create one!");
            repository.save(new SUser("demo", "用于演示", "13011111111", "demo@wdl.com", "VIEW", "demo"));
        }

        // fetch all customers
        LOG.info("Customers found with findAll():");
        LOG.info("-------------------------------");
        for (SUser user : repository.findAll()) {
            LOG.info(user.toString());
        }
    }
}
